<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>async/await</title>
  </head>
  <body>
    <script>
      /*
            async/await
                -可以代替生成器处理异步方案而且比generator好用
                -目前await只能在async中使用
          */

      //异步方案
      function ajax(url) {
        return new Promise((resolve, reject) => {
          const xhr = new XMLHttpRequest();
          xhr.open("get", url);
          xhr.responseType = "json";
          xhr.onload = function () {
            if (this.status === 200) {
              resolve(this.response);
            } else {
              reject(new Error(this.statuseText));
            }
          };
          xhr.send();
        });
      }
      async function main() {
        const res = await ajax("./nodes/data.json");
        console.log(res);
        const res2 = await ajax("./nodes/data1.json");
      }
      main();
    </script>
  </body>
</html>
